import { Component, EventEmitter } from '@angular/core';
import { PopoverController } from 'ionic-angular';
  
import { Utils } from '../../providers/providers';

@Component({
  selector: 'search-condition',
  templateUrl: 'search-condition.html',
  inputs: ['ngParam'],
  outputs: ['ngParamChange', 'query'],
})
export class SearchConditionComponent {

  private ngParamChange: any = new EventEmitter();
  private query: any = new EventEmitter();
  private ngParam: object;

  constructor(public popoverCtrl: PopoverController, public utils: Utils) {}
  
  ngOnInit () {}

  openCustPopover() {
    let popover = this.popoverCtrl.create('CustomerPopoverPage', 
      { data: this.ngParam }, { cssClass:'cust-list-popover' });
    popover.onDidDismiss((item) => {
      if(item) {
        for (let k in this.ngParam) {
          if(k == 'customTypeId') {
            this.ngParam[k] = (item == -1 ? '' : item.custId);
          }
        }
        this.ngParamChange.emit(this.ngParam);
        this.query.emit();
      }
    });
    popover.present();
  }

  openReductionPopover() {
    let popover = this.popoverCtrl.create('ReductionRatioPopoverPage', 
      {}, { cssClass:'cust-list-popover' });
    popover.onDidDismiss((item) => {
      if(item) {
        for (let k in this.ngParam) {
          if(k == 'downId') {
            this.ngParam[k] = (item == -1 ? '' : item.downId);
          }
        }
        this.ngParamChange.emit(this.ngParam);
        this.query.emit();
      }
    });
    popover.present();
  }

}
